<template>
  <div>
    <!--条件筛选-->
    <BreakSpace label="基本" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel ref="topfilter" :list="topFilterList" :cols="4" labelWidth="100px" @onCollapse="collapseHandle" :isSubmitBtn="false"></FormPanel>
    <BreakSpace label="售后维修" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel ref="topfilterRepair" :list="topfilterRepair" :cols="4" labelWidth="100px" @onCollapse="collapseHandle" :isSubmitBtn="false"></FormPanel>
    <BreakSpace label="库存(含税）单位 元" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel ref="topfilterStock" :list="topfilterStock" :cols="4" labelWidth="100px" @onCollapse="collapseHandle" :isSubmitBtn="false"></FormPanel>
    <BreakSpace label="经销商满足率" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel ref="topfilterSatisfaction" :list="topfilterSatisfaction" :cols="4" labelWidth="100px" @onCollapse="collapseHandle" :isSubmitBtn="false"></FormPanel>
    <BreakSpace label="周转率" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel ref="topfilterTurn" :list="topfilterTurn" :cols="4" labelWidth="100px" @onCollapse="collapseHandle" :isSubmitBtn="false"></FormPanel>
    <!--操作 -->
    <ButtonArea :containerStyle="{ display: 'flex', justifyContent: 'center' }">
      <AjaxButton size="small" :click="getData.bind(this, 50001001)">获取油月报</AjaxButton>
      <AjaxButton size="small" :click="getData.bind(this, 50001002)">获取电月报</AjaxButton>
      <AjaxButton size="small" :click="uploadData">提交</AjaxButton>
    </ButtonArea>
    <!-- 列筛选-->
    <!-- :fetchapi="fetchApi" -->
    <!--列操作-->
    <!-- <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <AjaxButton size="small" type="danger" :click="removeHandler">删除</AjaxButton>
      </template> -->
    <!-- 选择经销商 -->
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { uploadMonthReport, showMonthReport } from '@/api/part/report/partreport';
import moment from 'moment';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { dealerInfoByAllInfo } from '@/api/part/allocation';
import { arrayObjDeWeight } from '@/utils/index';
import searchDealerPanel from '@/pages/repair/mileageDateChange/mileageApprove/searchDealerPanel';
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { forDates } from '../../../../utils/validate';
import { notifyAction } from '../../../../utils';
// import { getUsedCardQualitys } from '@/api/usedcar/basic/qaproduct'; // 页面调用的的方法,对应的路径连接后台

export default {
  name: 'zxUsedCardQuality',
  components: {
    searchDealerPanel
  },
  mixins: [dictionary, dealerMixin],
  data() {
    this.conf = { add: '新增', delete: '删除', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      topfilterRepair: this.createTopfilterRepair(),
      topfilterStock: this.createTopfilterStock(),
      topfilterSatisfaction: this.createTopfilterSatisfaction(),
      topfilterTurn: this.createTopfilterTurn(),
      fetchParams: { xhrAbort: true },
      visible: false,
      visible1: false,
      dialoglabel: '',
      // fetchApi: getUsedCardQualitys,
      handleType: '',
      formData: {},
      dealerAction: { type: '', visible: false, label: '选择经销商', data: null },
      tableData: [],
      ids: []
    };
  },
  mounted() {
    // 分页
    this.getAllOwnerCode();
    this.getSalerList();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          label: '经销商代码',
          fieldName: 'OWNER_CODE',
          disabled: true,
          style: { width: '150px' },
          type: 'INPUT'
        },
        {
          label: '经销商名称',
          fieldName: 'ownerName',
          disabled: true,
          type: 'SELECT',
          style: { width: '150px' },
          itemList: []
        },
        {
          label: '年份',
          fieldName: 'reportYear',
          disabled: true,
          type: 'INPUT',
          style: { width: '150px' },
          filter: true
        },
        {
          label: '月份',
          fieldName: 'reportMonth',
          disabled: true,
          type: 'INPUT',
          style: { width: '150px' },
          filter: true
        },
        {
          label: '油电属性',
          fieldName: 'powerType',
          disabled: true,
          type: 'SELECT',
          itemList: [],
          style: { width: '150px' },
          filter: true
        },
        {
          label: '制单人',
          fieldName: 'createdBy',
          disabled: true,
          type: 'SELECT',
          style: { width: '150px' },
          itemList: []
        },
        {
          label: '制单时间',
          fieldName: 'createdAt',
          disabled: true,
          style: { width: '150px' },
          type: 'INPUT'
        }
      ];
    },
    createTopfilterRepair() {
      return [
        {
          label: '总进厂台次',
          fieldName: 'sumInOrder',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '保养进厂台次',
          fieldName: 'byInOrder',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '维修进厂台次',
          fieldName: 'repairInOrder',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '总产值',
          fieldName: 'sumAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        }
      ];
    },
    createTopfilterStock() {
      return [
        {
          label: '期末库存金额',
          fieldName: 'closeAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '期末库存品种',
          fieldName: 'partTypeClose',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '期初库存金额',
          fieldName: 'openAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '期初库存品种',
          fieldName: 'partTypeOpen',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '机油库存金额',
          fieldName: 'oilCostAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '机油库存品种',
          fieldName: 'oilTypeCounts',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '入库金额',
          fieldName: 'stockInAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '入库品种',
          fieldName: 'inTypeCounts',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '保养出库金额',
          fieldName: 'byOutAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '其他出库金额',
          fieldName: 'otherOutAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        }
      ];
    },
    createTopfilterSatisfaction() {
      return [
        {
          label: '终端配件一次满足率(%)',
          fieldName: 'oneRatePart',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '配件工单一次满足率(%)',
          fieldName: 'oneRateRepair',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        }
      ];
    },
    createTopfilterTurn() {
      return [
        {
          label: '周转率(%)',
          fieldName: 'turnOver',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '配件毛利润(元)',
          fieldName: 'partProfit',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '呆滞库存金额(元)',
          fieldName: 'dullCostAmount',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '呆滞库存比例(%)',
          fieldName: 'sluggishInventoryRate',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        },
        {
          label: '必备库存储备率(%)',
          fieldName: 'mustTypeRate',
          disabled: true,
          type: 'INPUT_NUMBER',
          style: { width: '150px' },
          initialValue: 0
        }
      ];
    },
    selectData(val) {
      this.tableData = val;
    },
    addHandle() {
      // 新建
      this.handleType = 'add';
      this.showHandler();
    },
    closeDealerDialog() {
      this.dealerAction.visible = false;
    },
    saveDealerDialog([params]) {
      this.dealerAction.visible = false;
      this.topFilterList.find(x => x.fieldName === 'ownerCode').initialValue = params.dealerCode;
      this.topFilterList.find(x => x.fieldName === 'ownerShortName').initialValue = params.dealerShortname;
    },
    // async removeHandler() {
    //   // 删除
    //   if (!this.tableData.length) {
    //     return this.$notify({
    //       label: '提示信息',
    //       message: '请选择数据',
    //       type: 'error'
    //     });
    //   }
    //   await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   });
    //   // console.log(999);
    //   await this.deleteDialog(); // 执行删除方法
    //   // console.log(999);
    // },
    // async deleteDialog() {
    //   // 取出数据id
    //   let res = {};
    //   this.ids = this.tableData.map(x => x.qualityId); // 获取选中页面的ID加到map集合中
    //
    //   res = await deleteUsedCardQualitys({ ids: this.ids.join(',') });
    //   // alert("aaaaaa"+res);
    //   // console.log(res);
    //   // 判断结果
    //   // alert("aaaaa"+res.resultCode+"aaa");
    //   if (res.resultCode === 200) {
    //     // alert(this.res);
    //     // console.log(res.data);
    //     if (res.data) {
    //       this.fetchParams = { ...this.fetchParams }; // 重复新加载
    //
    //       return this.$notify({
    //         // 提示信息
    //         label: '提示信息',
    //         message: '删除成功',
    //         type: 'success'
    //       });
    //     }
    //   }
    // },
    // editHandle(row) {
    //   this.handleType = 'edit'; // 编辑
    //   this.formData = row;
    //   this.showHandler();
    // },
    async getSalerList() {
      let emps = await getDealerUser();
      let obj = {};
      let data = arrayObjDeWeight(emps.data, 'userId');
      this.topFilterList.find(x => x.fieldName === 'createdBy').itemList = data.map(x => ({
        value: x.userId,
        text: x.employeeName
      }));
    },
    async getData(val) {
      let res = await showMonthReport({ powerType: val });
      if (res.resultCode == 200) {
        this.topFilterList.find(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.topfilterRepair.find(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.topfilterStock.find(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.topfilterSatisfaction.find(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.topfilterTurn.find(x => {
          x.initialValue = res.data[x.fieldName];
        });
      }
    },
    async uploadData() {
      let formData = this.$refs.topfilter.SUBMIT_FORM();
      let formDataRepair = this.$refs.topfilterRepair.SUBMIT_FORM();
      let formDataStock = this.$refs.topfilterStock.SUBMIT_FORM();
      let formDataSatisfaction = this.$refs.topfilterSatisfaction.SUBMIT_FORM();
      let formDataTurn = this.$refs.topfilterTurn.SUBMIT_FORM();
      let params = Object.assign(formData, formDataRepair, formDataStock, formDataSatisfaction, formDataTurn);
      console.log(params, 'params:::');
      let res = await uploadMonthReport(params);
      if (res.resultCode == 200) {
        notifyAction('提交成功', 'success');
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async exportHandle() {
      const [err, params] = await this.$refs.topfilter.GET_FORM_DATA();
      let map = JSON.parse(JSON.stringify(params));
      if (map.ownerCode && map.ownerCode != null && map.ownerCode.length > 0) {
        map.ownerCode = map.ownerCode.join("','");
        map.ownerCode = "'" + map.ownerCode + "'";
      }
      let date = '';
      if (map.month && map.month != null && map.month != '') {
        date = map.month.substring(0, 9);
      }
      await this.downHandleFile(exportMonthReport, map, '配件月报(' + date + ')');
    },
    // showHandler() {
    //   // 显示
    //   this.dialoglabel = `${this.conf[this.handleType]}新车保养费用申请单`;
    //   this.visible = true;
    // },
    async getAllOwnerCode() {
      let res = await dealerInfoByAllInfo({});
      console.log(res, 'dealerInfoByAllInfo');
      let data = arrayObjDeWeight(res.data, 'dealerCode');
      this.topFilterList.find(item => item.fieldName == 'ownerName').itemList = data.map(x => ({ text: x.dealerName, value: x.dealerCode }));
      this.topFilterList.find(item => item.fieldName == 'powerType').itemList = this.createDictList('5000');
    },
    closeHandler(val, type) {
      // 关闭
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
